<?php // $Id: chat.php 11215 2007-02-26 10:29:11Z elixir_julian $
/*
==============================================================================
	Dokeos - elearning and course management software

	Copyright (c) 2004 Dokeos S.A.
	Copyright (c) 2003 Ghent University (UGent)
	Copyright (c) 2001 Universite catholique de Louvain (UCL)
	Copyright (c) Olivier Brouckaert

	For a full list of contributors, see "credits.txt".
	The full license can be read in "license.txt".

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	See the GNU General Public License for more details.

	Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
==============================================================================
*/
/**
==============================================================================
*	Frameset of the Chat tool
*
*	@author Olivier Brouckaert
*	@package dokeos.chat
==============================================================================
*/
define('FRAME','list');
$language_file = array ('chat');
include('../inc/global.inc.php');
$nameTools=get_lang('Chat');
api_block_anonymous_users();
//if ($_GET["origin"] != 'whoisonline') {
//	api_protect_course_script();
//}
//else
//{
	$origin = $_SESSION['origin'];
	$target = $_SESSION['target'];
	$_SESSION['origin']=$_GET["origin"];
	$_SESSION['target']=$_GET["target"];
//}
/* ============================================================================== 
  			TRACKING
==============================================================================  */
include('../inc/lib/events.lib.inc.php');
event_access_tool(TOOL_CHAT);

$used_stylesheet=api_get_setting('stylesheets');

switch($used_stylesheet){
	case 'default' : 
		$footer_size = 48;
		break;
	case 'academica' : 
		$footer_size = 140;
		break;
	case 'corporativa' : 
		$footer_size = 60;
		break;
	case 'baby' : 
		$footer_size = 50;
		break;
	default : 
		$footer_size = 48;
		break;
}
include('header_frame.inc.php');
?>
<h1>Your chat rooms</h1>
<?php
global $_user;
// New room?
if($_POST['btnSubmit'])
{
	$name = Database::escape_string(trim($_POST['txtName']));
	if($name)
	{
		$table = Database::get_main_table("chatroom");
		$query = "INSERT INTO $table(name, creator_id) VALUES(?, ?);";
		$link = api_mysqli_connect();
		if($stmt = mysqli_prepare($link, $query))
		{
			mysqli_stmt_bind_param($stmt, "si", $name, intval($_user['user_id']));
			if(mysqli_stmt_execute($stmt))
			{
				echo "<p>Chat room created.</p>";
			}
			else
			{
				echo "<p>Chat room not created.</p>";
			}
		}
		//$result = api_sql_query($query, __FILE__, __LINE__);
		//if($result)
		//	echo "<p>Chat room created.</p>";
		//else
		//	echo "<p>Chat room not created.</p>";
	}
}
// Get chat rooms
if(isset($_user['user_id']))
{
	$table = Database::get_main_table("chatroom");
	$query = "SELECT id, name FROM $table WHERE creator_id = $_user[user_id];";
	$result=api_sql_query($query,__FILE__,__LINE__);
	if($result && mysql_num_rows($result))
	{
		while($row = mysql_fetch_array($result))
		{
			echo "<p><a href=\"chat.php?id=$row[id]\" target=\"_blank\">$row[name]</a></p>";
		}
	}
	else
	{
		echo "<p>You don't have any chat rooms.</p>";
	}
}
?>
<h2>Create new chat room</h2>
<form name="frmCreate" method="post">
	<table>
		<tr>
			<td>Chat room name (no more than 20 chars):</td>
			<td><input name="txtName" type="text" maxlength="20" /></td>
			<td><input name="btnSubmit" type="submit" value="Create" /></td>
		</tr>
	</table>
</form>
<h2>Chat invitations</h2>
<?php
// Ignoring invitation?
if(isset($_GET['act']) && isset($_GET['id']) && $_GET['act'] == "ignore")
{
	$chatroomId = intval($_GET['id']);
	$chatroomUser = Database::get_main_table("chatroom_user");
	$query = "DELETE FROM $chatroomUser WHERE user_id = $_user[user_id] AND chatroom_id = $chatroomId";
	api_sql_query($query,__FILE__,__LINE__);
	echo "<p>Invitation ignored.</p>";
}
$chatroom = Database::get_main_table("chatroom");
$chatroomUser = Database::get_main_table("chatroom_user");
$user = Database::get_main_table(TABLE_MAIN_USER);
$query = "SELECT c.id, c.name, u.lastname, u.firstname FROM $chatroom c, $chatroomUser cu, $user u WHERE c.id = cu.chatroom_id AND c.creator_id = u.user_id AND cu.accepted = 'n' AND cu.user_id = $_user[user_id];";
$result = api_sql_query($query,__FILE__,__LINE__);
if($result && mysql_num_rows($result) > 0)
{
	echo "<table>";
	echo "<tr>
		<th>Chatroom name</th>
		<th>Creator</th>
		<th>Action</th>
	</tr>";
	while($row  = mysql_fetch_array($result))
	{
		echo "<tr>
			<td>$row[name]</td>
			<td>$row[firstname] $row[lastname]</td>
			<td><a href=\"chat.php?id=$row[id]&act=accept\" target=\"_blank\" onclick=\"location.reload(true);\">Accept</a> <a href=\"?act=ignore&id=$row[id]\" onclick=\"return confirm('Are you sure to ignore this invitation?');\">Ignore</a></td>
		</tr>";
	}
	echo "</table";
}
else
{
	echo "<p>You don't have any chat invitations right now.</p>";
}
?>
